package com.example.cofeed.activity;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;

import com.blankj.utilcode.util.ObjectUtils;
import com.example.cofeed.adapter.ReplyCommentsAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseListActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.entity.MessageNotification;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class ReplyCommentsActivity extends BaseListActivity<MessageNotification> {
    private ReplyCommentsAdapter mReplyCommentsAdapter;
    private int mType;

    @Override
    protected void bindRecyclerView(RecyclerView recLv) {
        //0-咨询列表，1-回复评论
        mType = getIntent().getIntExtra(IntentKeyConstant.TYPE, 0);

        recLv.setLayoutManager(new LinearLayoutManager(this));
        mReplyCommentsAdapter = new ReplyCommentsAdapter();
        recLv.setAdapter(mReplyCommentsAdapter);

        mReplyCommentsAdapter.setType(mType);
        if (mType == 1) {
            setMainTitle("评论回复");
        } else if (mType == 2) {
            setMainTitle("商机消息");
        } else {
            setMainTitle("咨询消息");
        }

        mReplyCommentsAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            MessageNotification messageNotification = (MessageNotification) adapter.getData().get(position);

            final EditText inputServer = new EditText(mContext);
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请输入回复内容").setIcon(android.R.drawable.ic_dialog_info).setView(inputServer)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String input = inputServer.getText().toString().trim();
                    if (ObjectUtils.isEmpty(input)) {
                        showToast("请输入回复内容");
                        return;
                    }
                    dialog.dismiss();
                    ApiService.supply_asking_reply(this, messageNotification.id, input, new DialogCallback<LzyResponse<Object>>(ReplyCommentsActivity.this) {
                        @Override
                        public void onSuccess(Response<LzyResponse<Object>> response) {
                            autoRefresh();

                        }
                    });

                }
            });
            builder.show();

        });

    }

    @Override
    protected void loadData(int page, int pageCount) {
        if (mType == 1) {
            ApiService.comment_user(this, page, new DialogCallback<LzyResponse<List<MessageNotification>>>() {
                @Override
                public void onSuccess(Response<LzyResponse<List<MessageNotification>>> response) {
                    onGetDataSuc(response.body().data);
                }
            });
        } else if (mType == 2) {
            ApiService.message_business(this, page, new DialogCallback<LzyResponse<List<MessageNotification>>>() {
                @Override
                public void onSuccess(Response<LzyResponse<List<MessageNotification>>> response) {
                    onGetDataSuc(response.body().data);
                }
            });
        } else {
            ApiService.message_supply(this, page, new DialogCallback<LzyResponse<List<MessageNotification>>>() {
                @Override
                public void onSuccess(Response<LzyResponse<List<MessageNotification>>> response) {
                    onGetDataSuc(response.body().data);
                }
            });
        }
    }

    @Override
    protected List<MessageNotification> getData() {
        return mReplyCommentsAdapter.getData();
    }

    @Override
    protected void bindData(boolean isRefresh, List<MessageNotification> list) {
        if (isRefresh) {
            mReplyCommentsAdapter.setNewData(list);
        } else {
            mReplyCommentsAdapter.addData(list);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
